// pages/realname/realname.js

const app = getApp()
const pathUrl = app.globalData.pathUrl;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    proAry: ['实名认证', '发起公证', '活体检测', '签署完成'],

    imageUrl01: "", //身份证人像面
    imageUrl02: "", //身份证国徽面
    isCheck: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var orderNo = wx.getStorageSync('orderNo')
    var userId = wx.getStorageSync('userId')
    var loginInfo = JSON.parse(wx.getStorageSync('loginInfo'));
    console.log(loginInfo)
    var name = loginInfo.records[0].query13;
    console.log(name)
    that.setData({
      orderNo: orderNo,
      userId: userId,
      loginInfo: loginInfo,
      name: name,

    })
  },
  smfw:function(){
    wx.navigateTo({
      url: '/pages/contract/contract',
    })
  },
  // 上传身份证人像面
  getEnclosurePic: function(e) {
    var that = this;
    var sbtype = e.currentTarget.dataset.sbtype;
    var orderNo = that.data.orderNo;
    var userId = that.data.userId;
    var name = that.data.name;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        });
        var uploadImgCount = 0;
        console.log(tempFilePaths);
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: pathUrl + '/gzgl/fxyGzgl07/upload?filefrom=1000&filetype=1001&userid=' + userId + '&orderno=' + orderNo,
            filePath: tempFilePaths[i],
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function(res) {
              var data = JSON.parse(res.data);
              console.log(data);
              console.log(JSON.parse(data.result))
              var fileId = JSON.parse(data.result).fileId
              console.log(fileId)
              wx.setStorageSync('fileId', fileId)
              console.log(pathUrl + '/sys/common/view/' + data.message)
              that.setData({
                imageUrl01: pathUrl + '/sys/common/view/' + data.message
              })
              wx.hideToast();
            },
            fail: function(res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false
              });
            }
          });
        }

      },
    });
  },
  getEnclosurePic1: function(e) {
    var that = this;
    var sbtype = e.currentTarget.dataset.sbtype;
    var orderNo = that.data.orderNo;
    var userId = that.data.userId;
    console.log(orderNo)
    console.log(userId)
    var name = that.data.name;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        });
        var uploadImgCount = 0;
        console.log(tempFilePaths);
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: pathUrl + '/gzgl/fxyGzgl07/upload?filefrom=1000&filetype=1002&userid=' + userId + '&orderno=' + orderNo,
            filePath: tempFilePaths[i],
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function(res) {
              var data = JSON.parse(res.data);
              console.log(data);
              console.log(JSON.parse(data.result))
              var fileId1 =JSON.parse(data.result).fileId
              console.log(fileId1)
              wx.setStorageSync('fileId1', fileId1)
              console.log(pathUrl + '/sys/common/view/' + data.message)
              that.setData({
                imageUrl02: pathUrl + '/sys/common/view/' + data.message
              })
              wx.hideToast();
            },
            fail: function(res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false
              });
            }
          });
        }

      },
    });
  },
  // 是否阅读并同意协议
  xieyi: function(e) {
    console.log(e.detail.value)
    if (e.detail.value == '') {
      this.setData({
        isCheck: false
      });
    } else {
      this.setData({
        isCheck: true
      });
    }
    this.changeLoginBtnStatus();
  },
  // 改变登陆按钮状态
  changeLoginBtnStatus: function() {
    if (this.data.imageUrl01 && this.data.imageUrl02 && this.data.isCheck) {
      this.setData({
        loginBtnStatus: true
      });
    } else {
      this.setData({
        loginBtnStatus: false
      });
    }
  },
  goRealNameDtl: function() {
    console.log('aa');
    var that = this;
    console.log(that.data.isCheck)
    if (!that.data.imageUrl01) {
      wx.showModal({
        title: '提示',
        content: '缺少身份证人像面',
        showCancel:false
      });
      return false;

    }
    if (!that.data.imageUrl02) {
      wx.showModal({
        title: '提示',
        content: '缺少身份证国徽面',
        showCancel: false
      });
      return false;
    }
    if (!that.data.isCheck) {
      wx.showModal({
        title: '提示',
        content: '您未同意协议',
        showCancel: false
      });
      return false;
    }
    wx.navigateTo({
      url: '../realnamedtl/realnamedtl'
    });


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },



  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})